

#include "animation.h"
#include "draw.h"
 #include "font.h"
   #include "init.h"
     #include "input.h"
    #include "main.h"
      #include "map.h"
         #include "menu.h"
         #include "monster.h"
            #include "player.h"
             #include "sounds.h"


  void initializePlayer(void)
   {
     switch (jeu.level)
       {
           case 1:
    changeAnimation(&player, "graphics/walkright1.png");
         break;
           case 2 :
            changeAnimation(&player, "graphics/Fly1.png");
            break;
           case 3 :
            changeAnimation(&player, "graphics/walkright3.png");
            break;
       }
    player.direction = RIGHT;
    player.etat = IDLE;
if(jeu.level!=2)
    {player.x = 0;
    player.y = 0;
    player.onGround = 0;
    }
    else
    {
        player.x = 0;
    player.y = SCREEN_HEIGHT/2;
    }

    player.w = PLAYER_WIDTH;
    player.h = PLAYER_HEIGTH;


    player.timerMort = 0;


    jeu.nombreMonstres = 0;

   }


  void updatePlayer(void)
 {

  if (player.timerMort == 0)
  {
   if(jeu.level!=2)
   {
       player.dirX = 0;
    player.dirY += GRAVITY_SPEED;

    if (player.dirY >= MAX_FALL_SPEED)
    {
        player.dirY = MAX_FALL_SPEED;
    }
   }
    else
        player.dirX=2;

if(jeu.level==2)
     {
        player.dirY += 0.4;

    if (player.dirY >= 5.75)
        player.dirY = 5.75;

         if(input.jump==1)
           {
                player.dirY-=1.2;
                 if (player.dirY <=-4)
                 player.dirY =-4;
                if(player.etat!=FLY)
               {
                   player.etat=FLY;
                changeAnimation(&player,"graphics/Fly1.png");
            player.frameNumber=0;

               }
           }

        else if(input.jump==0)
          {
              if(player.etat!=IDLE)
              {
                  player.etat=IDLE;
            changeAnimation(&player,"graphics/IdleFly.png");
            player.frameNumber=0;
              }
          }
      }
    else
  {

    if (input.left == 1)
    {
        player.dirX -= PLAYER_SPEED;
        player.direction = LEFT;

        if(player.etat != WALK_LEFT && player.onGround == 1)
        {
            player.etat = WALK_LEFT;
                 switch (jeu.level)
       {
           case 1:
    changeAnimation(&player, "graphics/walkleft1.png");
         break;
           case 3 :
            changeAnimation(&player, "graphics/walkleft3.png");
            break;
       }
            player.frameNumber = 0;
        }
    }

    else if (input.right == 1)
    {
        player.dirX += PLAYER_SPEED;
        player.direction = RIGHT;

        if(player.etat != WALK_RIGHT && player.onGround == 1)
        {
            player.etat = WALK_RIGHT;
                switch (jeu.level)
       {
           case 1:
    changeAnimation(&player, "graphics/walkright1.png");
         break;
           case 3 :
            changeAnimation(&player, "graphics/walkright3.png");
            break;
       }
            player.frameNumber = 0;
        }
    }

    else if(input.right == 0 && input.left == 0 && player.onGround == 1)
    {

        if(player.etat != IDLE)
        {
            player.etat = IDLE;

            if(player.direction == LEFT)
            {
                     switch (jeu.level)
       {
           case 1:
    changeAnimation(&player, "graphics/IdleLeft1.png");
         break;
           case 3 :
            changeAnimation(&player, "graphics/IdleLeft3.png");
            break;
       }
                player.frameNumber = 0;
            }
            else
            {
            switch (jeu.level)
       {
           case 1:
    changeAnimation(&player, "graphics/IdleRight1.png");
         break;
           case 3 :
            changeAnimation(&player, "graphics/IdleRight3.png");
            break;
       }
                player.frameNumber = 0;
            }

        }

    }

    if (input.jump == 1 && player.onGround)
    {
        player.dirY = -JUMP_HEIGHT;
        player.onGround = 0;

    }
        if(input.enter == 1)
    {
        jeu.onMenu = 1;
        jeu.menuType = PAUSE;
        input.enter = 0;
    }
    if(player.onGround == 0)
    {
        if(player.direction == RIGHT && player.etat != JUMP_RIGHT)
        {
            player.etat = JUMP_RIGHT;
                        switch (jeu.level)
       {
           case 1:
    changeAnimation(&player, "graphics/JumpRight1.png");
         break;
           case 3 :
            changeAnimation(&player, "graphics/JumpRight3.png");
            break;
       }
            player.frameNumber = 0;
        }
        else if(player.direction == LEFT && player.etat != JUMP_LEFT)
        {
            player.etat = JUMP_LEFT;
         switch (jeu.level)
       {
           case 1:
    changeAnimation(&player, "graphics/JumpLeft1.png");
         break;
           case 3 :
            changeAnimation(&player, "graphics/JumpLeftt3.png");
            break;
       }
            player.frameNumber = 0;
        }
    }
  }

    mapCollision(&player);
    centerScrollingOnPlayer();

  }

    if (player.timerMort > 0)
    {
        player.timerMort--;

        if (player.timerMort == 0)
        {
            jeu.vies--;
            if(jeu.vies < 0)
            {
                jeu.onMenu = 1;
                jeu.menuType = START;
            }

            initializePlayer();
            changeLevel();
        }
    }

  }

 void centerScrollingOnPlayer(void)
 {

    map.startX = player.x - (SCREEN_WIDTH / 2);

    if (map.startX < 0)
    {
        map.startX = 0;
    }

    else if (map.startX + SCREEN_WIDTH >= map.maxX)
    {
        map.startX = map.maxX - SCREEN_WIDTH;
    }

    map.startY = player.y - (SCREEN_HEIGHT / 2);

    if (map.startY < 0)
    {
        map.startY = 0;
    }

    else if (map.startY + SCREEN_HEIGHT >= map.maxY)
    {
        map.startY = map.maxY - SCREEN_HEIGHT;
    }
 }

    void getItem(void)
{
    jeu.etoiles++;

    if ( jeu.etoiles >= 100 )
    {
        jeu.etoiles = 0;
        jeu.vies++;
    }
}



